<template>
<el-dialog title="上架明细" :close-on-click-modal="false" :visible.sync="visible" width="90%">
    <el-table :data="dataForm.bookList" border style="width: 100%;">
        <el-table-column prop="detailId" header-align="center" align="center" label="副本编号" width="80"></el-table-column>
        <el-table-column header-align="center" align="center" label="副本状态" width="80">
            <template slot-scope="scope">{{scope.row.status == 2?'待上架':''}}</template>
        </el-table-column>
        <el-table-column prop="storeName" header-align="center" align="center" label="仓库" width="80"></el-table-column>
        <el-table-column header-align="center" align="center" label="入库时间" width="100">
            <template slot-scope="scope">{{scope.row.storeDate | formatYearMonth}}</template>
        </el-table-column>
        <el-table-column prop="num" header-align="center" align="center" label="上架位置" width="100">
            <template slot-scope="scope">
                <el-input v-model="scope.row.upLocation"></el-input>
            </template>
        </el-table-column>
        <el-table-column prop="img" header-align="center" align="center" label="封面" width="120">
            <template slot-scope="scope">
                <img v-if="scope.row.imgUrl" style="width: 100px; height: 100px" :src="scope.row.imgUrl" />
                <span v-else></span>
            </template>
        </el-table-column>
        <el-table-column prop="name" header-align="center" align="center" label="图书名称"></el-table-column>
        <el-table-column prop="type" header-align="center" align="center" label="分类" width="50"></el-table-column>
        <el-table-column prop="isbn" header-align="center" align="center" label="ISBN" width="130"></el-table-column>
        <el-table-column prop="author" header-align="center" align="center" label="作者"></el-table-column>
        <el-table-column prop="press" header-align="center" align="center" label="出版社"></el-table-column>
        <el-table-column prop="publishDate" header-align="center" align="center" label="出版时间" width="100">
            <template slot-scope="scope">{{scope.row.publishDate | formatYearMonth}}</template>
        </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">上架确认</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: 0,
                bookList: []
            },
        };
    },
    methods: {
        init(id) {
            this.visible = true;
            this.$nextTick(() => {
                this.dataForm.id = id;
                if (this.dataForm.id) {
                    this.$http({
                        url: this.$http.adornUrl(
                            `/generator/tdelivery/info/${this.dataForm.id}`
                        ),
                        method: "get",
                        params: this.$http.adornParams()
                    }).then(({
                        data
                    }) => {
                        if (data && data.code === 0) {
                            console.log(data.data.bookList);
                            this.dataForm.bookList = data.data.bookList;
                            this.dataForm.bookList.forEach(function (item) {
                                if (item.img) {
                                    item.imgUrl =
                                        "http://localhost:8080/wenyuan/generator/tbook/img/" + item.id;
                                }
                            });
                        }
                    });
                }
            });
        },
        // 表单提交
        dataFormSubmit() {

            let flag = true;
            this.dataForm.bookList.forEach(function (item) {
                if (!item.upLocation) {
                    flag = false;
                }
            });
            if (!flag) {
                this.$message({
                    message: "请填写上架位置",
                    type: "error",
                    duration: 1500
                });
                return;
            }

            this.$http({
                url: this.$http.adornUrl(`/generator/tputwayapplication/up/` + this.dataForm.id),
                method: "post",
                data: this.dataForm.bookList
            }).then(({
                data
            }) => {
                if (data && data.code === 0) {
                    this.$message({
                        message: "上架成功",
                        type: "success",
                        duration: 1500,
                        onClose: () => {
                            this.visible = false;
                            this.$emit("refreshDataList");
                        },
                    });
                } else {
                    this.$message.error(data.msg);
                }
            });
        },
    },
};
</script>
